from BacktestManager import BacktestManager
from TestStrategy import TestStrategy
from BenchmarkStrategy import BenchmarkStrategy
import os
import time

# 读取行情文件列表
data_path = './tickdata/'
data_files = os.listdir(data_path)
for i in range(len(data_files)):
    data_files[i] = data_path + data_files[i]

# 设置初始仓位
portfolio = {'TestStrategy': {'600600': {'sellable_position': 100,
                                         'unsellable_position': 0,
                                         'locked_position': 0},
                              '300416': {'sellable_position': 200,
                                         'unsellable_position': 0,
                                         'locked_position': 0},
                              'cash': 20000},
             'Benchmark': {'600600': {'sellable_position': 100,
                                     'unsellable_position': 0,
                                     'locked_position': 0},
                          '300416': {'sellable_position': 200,
                                     'unsellable_position': 0,
                                     'locked_position': 0},
                          'cash': 20000},
             }

# 开始回测
backtester = BacktestManager(data_files, portfolio)
backtester.strategies_initer = [TestStrategy, BenchmarkStrategy]
backtester.start_back_test()

# 输出回测结果
time.sleep(3)
print('\n\n====================')
mv_seq = backtester.market_value_sequence
print('date\t\tTestStrategy\t\tBenchMark')
for file in mv_seq.keys():
    print(file[-14:-4], end='\t\t')
    print('%.2f'%mv_seq[file]['TestStrategy'], end='\t\t')
    print('%.2f'%mv_seq[file]['Benchmark'])

